#!/bin/bash

#exception: no need to trigger
if [ "x${GERRIT_PROJECT}" == "xeuleros/third_party/open_source/kernelspace/linux-4.19" ];then
    echo "Kernel update ,skip. Otherwise trigger obs manually"
    exit 0
fi
#The flag which reflects the status of CI_PROJRCT
build_status="true"
echo '' > ci_project_build_result
#get server ip from the url configed in web
jenkins_server_ip=$(echo "${JENKINS_URL}" | awk -F '/' '{print $3}')
#check the status of CI_PROJECT
while true
do
    curl http://${jenkins_server_ip}/jenkins/view/EulerOS_V2R8C00/job/EulerOS_V2R8C00-build-aarch64/lastBuild/api/xml > ci_project_build_result
    build_status=$(cat ci_project_build_result | awk -F '<building>' '{print $2}'| awk -F '</building>' '{print $1}')
    if [ "x$(cat ci_project_build_result)" != "xtrue" ];then
       break
    fi
done
git clone http://dgggit09-rd.huawei.com/a/euleros/tools/obs_meta -b "${GERRIT_BRANCH}"
cd ./obs_meta/projects
#find the packages that have the source commited this time,then remoterun them
grep "${GERRIT_PROJECT}" * -lR | grep -v -E "_sources$" | uniq | while read line
do
    item="${line%/*}"
    osc service remoterun "${item}"
    set +e
    osc service remoterun "SAN_${item}"
    set -e
done
cd -
rm -rf obs_meta
rm -f ci_project_build_result
